#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 10

void Swap(int*px, int* py)
{
	int tmp = *px;;
	*px = *py;
	*py = tmp;
}
void Print(int* a, int n)
{
	for (int i = 0; i < n; ++i)
		printf("%d ", a[i]);
	printf("\n");
}
void selectSort(int*a, int n)
{
	int left = 0, right = n;
	while (left < right)
	{
		int mini = left;
		int maxi = right;
		for (int i = left; i <= right; ++i)
		{
			if (a[i] < a[mini])
				mini = i;
			if (a[i] >= a[maxi])
				maxi = i;
		}
		Swap(&a[mini], &a[left]);
		if (maxi == mini)
			maxi = mini;
		Swap(&a[maxi], &a[right]);
		++left;
		--right;
	}
}

int search(int* a, int n, int search_a)
{
	int left = 0;
	int right = n - 1;
	while (left <= right)
	{
		int index = (left + right) / 2;
		if (search_a < a[index])
			right = index - 1;
		else if (search_a > a[index])
			left = index + 1;
		else
			return index;
	}
	return -1;
}
int REsearch(int* a, int left, int right, int search_a)
{
	if (left > right)
		return -1;
	int mid = (left + right) / 2;
	if (search_a < a[mid])
		return REsearch(a, left, mid - 1, search_a);
	else if (search_a > a[mid])
		return REsearch(a, mid + 1, right, search_a);
	else
		return mid;
}

int main()
{
	int a[MAX] = { 12, 14, 52, 11, 9, 8, 4, 25, 4, 1 };
	//srand(time(0));
	//int i = 0;
	//while(i < MAX)
	//    a[i] = rand() % 30;
	selectSort(a, MAX - 1);
	int search_a = 0;;
	scanf("%d", &search_a);
	int index = 0;
	index = search(a, sizeof(a) / sizeof(a[0]), search_a);
	index = REsearch(a, 0, sizeof(a) / sizeof(a[0]), search_a);
	if (index == -1)
	{
		printf("未查找到此数\n");
	}
	else
	{
		printf("查有此数：%d，下标：%d\n", search_a, index);
	}
	return 0;
}
